/*
 * Resource.fx
 *
 * Created on 2009-09-08, 19:58:16
 */

package shmallarchitect.gui;


import javafx.scene.Group;
import javafx.scene.transform.Translate;

import javafx.scene.image.ImageView;

import javafx.scene.image.Image;

import javafx.scene.control.Button;

import javafx.scene.control.TextBox;

import shmallarchitect.java.Resource;
import javafx.scene.CustomNode;
import javafx.scene.Node;

import javafx.scene.text.Text;

import javafx.scene.paint.Color;
import javafx.scene.text.Font;

import javafx.scene.shape.Rectangle;


import java.lang.Integer;



/**
 * @author Administrator
 */

public var show_img:Boolean = false;
public var edit_resource:Boolean = false;




var resourceID_show: Text = Text {

        x: 5 y: 410 //wersja
        font: Font.font("Verdana", 12);
        fill: Color.WHITE
        content: bind  "ID of this resource: {sizeof(BaseLayout.resource_list)}"

        }

var warning: Text = Text {

        x: 5 y: 460 //wersja
        font: Font.font("Verdana", 11);
        fill: Color.web("#f78355")
        content: "Please be aware that larger images take longer to load in preview!"

        }

var preview_id: Text = Text {

        x: 550 y: 410
        font: Font.font("Verdana", 11);
        fill: Color.web("#f78355")
        content: bind "Resources in database: 0 - {sizeof(BaseLayout.resource_list)}"
        }

var checkid: Button = Button {
        text: "Show ID"
        action: function() {

        testid = java.lang.Integer.parseInt(idbox.text);

        }
        transforms: [ Translate {x: 620, y: 370 } ]
}

var idbox = TextBox {

        text: "";
        columns: 4

transforms: [ Translate {x: 550, y: 370 } ]

}

var addResource: Button = Button {
        text: "Add this resource to the Gallery"
        action: function() {
                    insert 
                        new Resource(sizeof(BaseLayout.resource_list),urlbox.text)
                    into BaseLayout.resource_list;
        }
        transforms: [ Translate {x: 40, y: 560 } ]
}

var checkimg: Button = Button {
        text: "Check Image"
        action: function() {

               testshow = urlbox.text;

        }
        transforms: [ Translate {x: 250, y: 420 } ]
}

var urlbox = TextBox {

        text: "";
        columns: 50

transforms: [ Translate {x: -20, y: 370 } ]

}

public function resize_width(a:Image, b:Integer):Integer {

        if(a.width > b) {
        return b }

        else { return a.width }

        }

public function resize_height(a:Image, b:Integer):Integer {

        if(a.height > b) {
        return b }

        else { return a.height }

        }

public function show_resource(id:Integer, x_:Integer, y_:Integer) {

var img = bind Image {
                   url: "{ BaseLayout.resource_list[id].getUrl() }";
                  }

  return Group {
                  content: [

                         ImageView {
                                    image: img;
                                    fitHeight: 200
                                    fitWidth: 200
                                    preserveRatio: true
                                    smooth: true
                                    cache: true
                                 }

                                 ]

                 visible: true;
                 transforms: [ Translate {x: 600+x_, y: 50+y_ } ]
                 };

}

public var testshow: String;
public var testid: Integer = -1;


var img = bind Image {
                   url: testshow;
                   preserveRatio: true
                  }

var img_by_id = bind Image {
                   url: BaseLayout.resource_list[testid].getUrl();
                   preserveRatio: true
                  }


var imgView = ImageView {           image: bind img;
                                    fitWidth: bind resize_width(img, 450)
                                    fitHeight: bind resize_height(img, 410)
                                    preserveRatio: true
                                    smooth: true
                                    cache: true

                                 }

var idView = ImageView {           image: bind img_by_id;
                                    fitWidth: bind resize_width(img, 450)
                                    fitHeight: bind resize_height(img, 410)
                                    preserveRatio: true
                                    smooth: true
                                    cache: true


                                 }


public function test_resource() {

  return Group {
                  content: [

                         imgView
                 ]

                 visible: true;
                 transforms: [ Translate {x: -30, y: -50 } ]
                 };

}

public function test_id() {

  return Group {
                  content: [

                         idView
                 ]

                 visible: true;
                 transforms: [ Translate {x: 500, y: -50 } ]
                 };

}

public class ResourceUtil extends CustomNode {

        public override function create(): Node {
                return Group {
                        content: [

                             Rectangle { x: -31, y: -51
                                              width: 452
                                              height: 412
                                              fill: Color.web("#373737")},

                             Rectangle { x: 499, y: -51
                                              width: 452
                                              height: 412
                                              fill: Color.web("#373737")},
                                              
                                urlbox, addResource, resourceID_show,
                                  checkimg, test_resource(), warning, preview_id,
                                  checkid, idbox, test_id(), 


                        ]
                  visible: bind edit_resource;
                  transforms: [ Translate {x: 40, y: 80 } ]
                };
        }
}



